<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">路线名称</label>
        <el-select v-model="query.routeId" placeholder="请选择" clearable style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in routes"
            :key="item.id"
            :label="item.routeName"
            :value="item.id"
          />
        </el-select>
        <!-- <label class="el-form-item-label">经度</label>
        <el-input v-model="query.lng" clearable placeholder="经度" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="路线名称" prop="routeId">
            <el-select v-model="form.routeId" placeholder="请选择" style="width: 270px;">
              <el-option
                v-for="item in routes"
                :key="item.id"
                :label="item.routeName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="地点名称">
            <el-input v-model="form.name" style="width: 270px;" />
          </el-form-item>
          <el-form-item label="经度" prop="lng">
            <el-input v-model="form.lng" style="width: 270px;">
              <el-button slot="prepend" icon="el-icon-search" @click.native="getMapPoint" />
            </el-input>
          </el-form-item>
          <el-form-item label="维度" prop="lat">
            <el-input v-model="form.lat" style="width: 270px;">
              <el-button slot="prepend" icon="el-icon-search" @click.native="getMapPoint" />
            </el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <el-dialog :close-on-click-modal="false" :visible.sync="closeLayer" title="地图" width="800px">
        <baidu-map :center="center" :zoom="zoom" style="height:508px" :scroll-wheel-zoom="true" @ready="handler" @click="getClickInfo" />
        <div slot="footer" class="dialog-footer">
          <!-- <el-button type="text" @click="crud.cancelCU">取消</el-button> -->
          <el-button type="primary" @click="getMapPointFalse">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="routeId" label="路线名称" align="center">
          <template slot-scope="scope">
            {{ getRouteName(scope.row.routeId) }}

          </template>
        </el-table-column>
        <el-table-column prop="name" label="地点名称" align="center" />
        <el-table-column prop="lng" label="经度" align="center" />
        <el-table-column prop="lat" label="维度" align="center" />
        <el-table-column prop="craeteTime" label="创建时间" align="center" />

        <el-table-column v-if="checkPer(['admin','routePont:edit','routePont:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudRoutePont from '@/api/routePont'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getAllRoute } from '@/api/route'

var point
const defaultForm = { id: null, routeId: null, lng: null, lat: null, name: null, craeteTime: null, updateTime: null }
export default {
  name: 'RoutePont',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '地点', url: 'api/routePont', idField: 'id', sort: 'craeteTime,desc', crudMethod: { ...crudRoutePont }})
  },
  data() {
    return {
      lng: 112.848085,
      lat: 35.494521,
      center: { lng: this.lng, lat: this.lat },
      zoom: 13,
      dateValue: null,
      weatherValue: null,
      content: null,
      map: null,
      BMap: null,
      closeLayer: false,

      permission: {
        add: ['admin', 'routePont:add'],
        edit: ['admin', 'routePont:edit'],
        del: ['admin', 'routePont:del']
      },
      rules: {
        routeId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        lng: [
          { required: true, message: '经度不能为空', trigger: 'blur' }
        ],
        lat: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'routeId', display_name: 'routeId' },
        { key: 'lng', display_name: '经度' }
      ],
      routes: []
    }
  },
  mounted() {
    this.route()
  },
  methods: {
    handler({ BMap, map }) {
      console.log(this.crud.data)
      this.BMap = BMap
      console.log(BMap)
      this.map = map
      this.map.enableScrollWheelZoom(true) // 开启鼠标滚轮缩放
      this.map.addControl(new BMap.NavigationControl()) // 添加平移缩放控件
      this.map.addControl(new BMap.ScaleControl()) // 添加比例尺控件
      this.map.addControl(new BMap.OverviewMapControl()) // 添加缩略地图控件
      point = new BMap.Point(this.lng, this.lat)
      this.map.centerAndZoom(point, 16)

      var marker = new BMap.Marker(point) // 创建标注
      this.map.addOverlay(marker) // 将标注添加到地图中
      var circle = new BMap.Circle(point, 6, { strokeColor: 'Red', strokeWeight: 6, strokeOpacity: 1, Color: 'Red', fillColor: '#f03' })
      this.map.addOverlay(circle)
      // this.map.setHeading(64.5);   //设置地图旋转角度
      // this.map.setTilt(73);       //设置地图的倾斜角度
      // this.map.setMapType(BMAP_EARTH_MAP);
      // this.map.setHeading(64.5)
    },
    getMapPoint() {
      this.closeLayer = true
    },
    getMapPointFalse() {
      this.closeLayer = false
    },

    getClickInfo(e) {
      this.map.clearOverlays()
      console.log(e.point.lng)
      console.log(e.point.lat)
      this.center.lng = e.point.lng
      this.center.lat = e.point.lat
      point = new this.BMap.Point(this.center.lng, this.center.lat)
      this.map.centerAndZoom(point, 16)

      var marker = new this.BMap.Marker(point) // 创建标注
      this.map.addOverlay(marker) // 将标注添加到地图中
      this.form.lng = e.point.lng
      this.form.lat = e.point.lat
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU]() {
      var point
      this.map.clearOverlays()
      point = new this.BMap.Point(this.form.lng, this.form.lat)
      this.map.centerAndZoom(point, 16)
      var marker = new this.BMap.Marker(point) // 创建标注
      this.map.addOverlay(marker) // 将标注添加到地图中
    },
    route() {
      getAllRoute().then(res => {
        this.routes = res.content
      })
    },
    getRouteName(id) {
      for (var i = 0; i < this.routes.length; i++) {
        if (id === this.routes[i].id) {
          return this.routes[i].routeName
        }
      }
    }
  }
}
</script>

<style scoped>

</style>
